package org.social.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.error.GMErrorCode;
import org.social.db.bean.GiftCode;
import org.social.db.bean.GiftPackage;
import org.social.db.cache.GiftCodeCache;
import org.social.db.cache.GiftPackageCache;
import org.social.manager.GiftCodeManager;
import org.utils.CommonUtil;

import com.dal.gm_api.api.IGiftCode;
import com.google.common.collect.Lists;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.tools.ToolError;

/**
 * Created by fxf on 2018-1-11.
 */
public class GiftCodeServer implements IGiftCode {

    public void deleteGiftCode(String code) {
        ToolError.isAndTrue(GMErrorCode.PARAM_ERR, "code为空", code == null || code.length() == 0);
        ToolError.isAndTrue(GMErrorCode.GIFT_CODE_INVALID, "无效礼包码", GiftCodeCache.me().getByCode(code) == null);
        GiftCodeManager.deleteGiftCode(code);
    }

    public String queryGiftCode(String code) {
        ToolError.isAndTrue(GMErrorCode.PARAM_ERR, "code为空", code == null || code.length() == 0);
        ToolError.isAndTrue(GMErrorCode.GIFT_CODE_INVALID, "无效礼包码", GiftCodeCache.me().getByCode(code) == null);
        GiftCode giftCode = GiftCodeManager.queryGiftCode(code);
        return giftCode.toJson();
    }

    public List<String> queryAllGiftCode(){
        List<GiftCode> all = GiftCodeCache.me().loadAll();
        List<String> result = Lists.newArrayList();
        all.forEach(code ->result.add(code.toJson()));
        return result;
    }


	@Override
	public void createGiftPackage(String name,String json,String des) {
		Map items = JsonManager.me().getJson().parse(json, Map.class);
		GiftPackage pack = new GiftPackage(0, name, json, des);
		pack.insert();
	}

	@Override
	public List<String> getAllGiftPackage() {
		List<GiftPackage> lists = GiftPackageCache.me().getCacheAll();
		List<String> dtos = new ArrayList<>(lists.size());
		for (GiftPackage giftPackage : lists) {
			dtos.add(giftPackage.toJson());
		}
		return dtos;
	}
	
	@Override
	public void addGiftCode(int count,String name,int giftPackageId, int limitCount, String platform, int channelId, Date startDate,Date endDate, String remark) {
		if (startDate != null && endDate != null) {
			ToolError.isAndTrue(GMErrorCode.PARAM_ERR, "时间无效,开始时间大于结束时间", startDate.after(endDate));	
		}
		
		ToolError.isAndTrue(GMErrorCode.PARAM_ERR, "时间无效,开始时间大于结束时间", startDate.after(endDate));
		
		GiftPackage giftPackage = GiftPackageCache.me().getById(giftPackageId);
		ToolError.isAndTrue(GMErrorCode.PARAM_ERR, "礼包不存在", giftPackage == null);
		
		for (int j = 0; j < count; j++) {
			String code = null;
			for (int i = 0; i < 10; i++) {
				code = CommonUtil.generateShortUuid();
				GiftCode giftCode =  GiftCodeCache.me().getByCode(code);
				if (giftCode == null) {
					break;
				}
			}
	        GiftCodeManager.addGiftCode(code, name, giftPackageId, limitCount, platform, channelId, startDate, endDate, remark);			
		}
	}
}
